package com.wuyou.single.common.security;

import com.wuyou.single.common.security.handler.AuthenticationEntryPointImpl;
import com.wuyou.single.common.security.handler.LogoutHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.DefaultLoginPageConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.annotation.Resource;

/**
 * @Description
 * @Author 无忧
 * @Date 2024/6/11 22:53
 */
@Slf4j
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    // 登录失败处理
    @Resource
    private AuthenticationEntryPoint unauthorizedHandler;

    @Resource
    private LogoutHandler logoutSuccessHandler;


    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        log.info("权限校验启动");
//        super.configure(http);

        http.
                // CSRF禁用
                        csrf().disable()
                // 1、认证失败处理
                .exceptionHandling()
                .authenticationEntryPoint(unauthorizedHandler)
                .and()
                // 基于token，所以不需要session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers("/login", "/register", "/captchaImage", "/doLogin", "/hello").anonymous()
                .antMatchers(
                        HttpMethod.GET,
                        "/",
                        "/*.html",
                        "/**/*.html",
                        "/**/*.css",
                        "/**/*.js",
                        "/profile/**"
                ).permitAll()
                .antMatchers("/swagger-ui.html").anonymous()
                .antMatchers("/swagger-resources/**").anonymous()
                // 除了以上请求，其他都需要鉴权认证
                .anyRequest().authenticated()
                .and()
                .headers().frameOptions().disable();
        // 禁用默认的表单登录
        http.formLogin().disable();
//                .logout().disable(); // 禁用默认的注销
        // 移除默认登录页
//            http.removeConfigurer(DefaultLoginPageConfigurer.class);


        http.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
    }

}
